﻿using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;


using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.Collections;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Globalization;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Collections.Generic;

namespace PeopleSearch.PeopleProfileSearch
{
    [ToolboxItemAttribute(false)]
    public partial class PeopleProfileSearch : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public PeopleProfileSearch()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            GetProfileInfo();
        }


        /// <summary>
        /// To Load the Profile data
        /// </summary>
        private void GetProfileInfo()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                    try
                    {
                        SPServiceContext context = SPServiceContext.GetContext(site);
                        UserProfileManager profileManager = new UserProfileManager(context);
                        var collection = new List<UserProfile>();
                        int cnt = 0;

                        //Get all the profile data from the current site instance
                        foreach (UserProfile ospUserProfile in profileManager)
                            collection.Add(ospUserProfile);

                        string[] profileinfo = new string[collection.Count];
                        string[] URLinfo = new string[collection.Count];
                        foreach (UserProfile ospUserProfile in collection)
                        {
                            profileinfo[cnt] = ospUserProfile.DisplayName;
                            URLinfo[cnt] = ospUserProfile.DisplayName + "~" + ospUserProfile.PersonalUrl;
                            cnt = cnt + 1;
                        }

                        //Add the dynamic script into the page for autocomplete process
                        var serializer = new JavaScriptSerializer();
                        var availableTags = string.Format("var availableTags = {0}", serializer.Serialize(profileinfo));
                        var availableURLTags = string.Format("var availableURLTags = {0}", serializer.Serialize(URLinfo));

                        ClientScriptManager cs = Page.ClientScript;
                        cs.RegisterClientScriptBlock(
                            this.GetType(),
                            "arrayDeclaration",
                            availableTags + ";" + availableURLTags + ";",
                            true
                        );

                    }

                    catch (System.Exception ex)
                    {
                        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("People Search", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);

                    }
                }
              });
         
          
        }

    }
}
